package com.megvii.corelib.net;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by linyue on 15/12/20.
 */
public class MegviiJsonRequest<T> extends Request<T>
{
    private Class<T> clazz;

    private Response.Listener listener = null;

    private MegviiHeaderListener headerListener = null;

    public MegviiJsonRequest(int method, String url, Response.Listener<T> listener,
            Response.ErrorListener errorListener, Class<T> clazz)
    {
        super(method, url, errorListener);
        //设置失败重试的参数
        this.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 10,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        this.listener = listener;
        this.clazz = clazz;
    }

    public void setHeaderListener(MegviiHeaderListener headerListener)
    {
        this.headerListener = headerListener;
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response)
    {
        try
        {
            if (headerListener != null && response.headers != null)
            {
                Iterator<String> iterator = response.headers.keySet().iterator();
                while (iterator.hasNext())
                {
                    String key = iterator.next();
                    String value = response.headers.get(key);
                    headerListener.onReceiveHeader(key, value);
                }
            }
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, "utf-8"));
            try
            {
                ObjectMapper mapper = new ObjectMapper();
                mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                T object = mapper.readValue(jsonString, clazz);
                return Response.success(object, HttpHeaderParser.parseCacheHeaders(response));
            }
            catch (JsonMappingException e)
            {
                return Response.error(new ParseError(e));
            }
            catch (JsonParseException e)
            {
                return Response.error(new ParseError(e));
            }
            catch (IOException e)
            {
                return Response.error(new ParseError(e));
            }
        }
        catch (UnsupportedEncodingException e)
        {
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(T response)
    {
        if (listener != null)
        {
            listener.onResponse(response);
        }
    }

    protected Map<String, File> getFileParams()
    {
        return null;
    }
}
